package com.cn.ccp.action.system;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.action.course.online.OnlineAction;
import com.cn.ccp.bean.FacultyBean;
import com.cn.ccp.bean.RoleBean;
import com.cn.ccp.bean.SpecialtyBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;

/**&
 * 课程授权管理
 * @author Administrator
 *
 */
public class CourseManageAction extends BaseAction{
	private Logger log = Logger.getLogger(OnlineAction.class);
	private SelectItem[] roleSelectItem ;
	private SelectItem[] facultySelectItem ;
	private SelectItem[] specialtySelectItem ;
	private SelectItem[] userSelectItem ;
	
	private String roleId = "" ;
	private String facultyId = ""  ;
	private String specialtyId = ""  ;
	private List<String> userList = null ;
	
	
	public void roleValueChange(){
		userList = new ArrayList<String>(); 
		userSelectItem = null;
		log.info("roleId---"+roleId);
		getUserData();
	}
	
	public void facultyValueChange(){
		userSelectItem = null ;
		userList = new ArrayList<String>();
		log.info("facultyId---"+facultyId);
		List<SpecialtyBean> slist = getSpecialtyService().getSpecialtyByFacultyId(Integer.parseInt(facultyId));
		specialtySelectItem = new SelectItem[slist.size()];
		for (int i = 0; i < slist.size(); i++) 
			specialtySelectItem[i] = new SelectItem(""+slist.get(i).getId() , slist.get(i).getSpecialtyname());
	}
	
	public void userValueChange(){
		getMenuManageService().insertUserRoleByCourse(roleId, userList);
	}

	public void specialtyValueChange(){
		getUserData();
	}
	
	
	private void getUserData(){
		if(facultyId != null  && !facultyId.equals("")
				&& specialtyId != null && !specialtyId.equals("")){
			List<UserBean> ulist = getUserService().getUserLIstFromRole(roleId, specialtyId);
			userList = new ArrayList<String>(); 
			for (int i = 0; i < ulist.size(); i++) 
				userList.add(""+ulist.get(i).getId());
			
			
			List<UserBean> ubL = getUserService().getUserLIstBySpecialty(specialtyId);
			userSelectItem = new SelectItem[ubL.size()];
			for (int i = 0; i < ubL.size(); i++) 
				userSelectItem[i] = new SelectItem(""+ubL.get(i).getId() ,ubL.get(i).getUserName());
		}
	}


	public SelectItem[] getRoleSelectItem() {
		if(roleSelectItem == null){
			List<RoleBean> roleList = getRoleService().queryAll();
			roleSelectItem = new SelectItem[roleList.size()];
			for (int i = 0; i < roleList.size(); i++) 
				roleSelectItem[i] = new SelectItem(""+roleList.get(i).getId() , roleList.get(i).getRolename());
		}
		return roleSelectItem;
	}

	public void setRoleSelectItem(SelectItem[] roleSelectItem) {
		this.roleSelectItem = roleSelectItem;
	}

	public SelectItem[] getFacultySelectItem() {
		if(facultySelectItem == null){
			List<FacultyBean> facultyList = getFacultyService().getAllFaculty();
			facultySelectItem = new SelectItem[facultyList.size()];
			for (int i = 0; i < facultyList.size(); i++) 
				facultySelectItem[i] = new SelectItem(""+facultyList.get(i).getId() , facultyList.get(i).getFacultyName());
		}
		return facultySelectItem;
	}

	public void setFacultySelectItem(SelectItem[] facultySelectItem) {
		this.facultySelectItem = facultySelectItem;
	}

	public SelectItem[] getSpecialtySelectItem() {
		if(specialtySelectItem == null){
			userSelectItem = null ;
			userList = new ArrayList<String>();
//			System.out.println("facultyId---"+facultyId);
			List<SpecialtyBean> slist = getSpecialtyService().getSpecialtyByFacultyId(Integer.parseInt(getFacultyId()));
			specialtySelectItem = new SelectItem[slist.size()];
			for (int i = 0; i < slist.size(); i++) 
				specialtySelectItem[i] = new SelectItem(""+slist.get(i).getId() , slist.get(i).getSpecialtyname());
		}
		if(specialtySelectItem == null)
			specialtySelectItem = new SelectItem[0];
		return specialtySelectItem;
	}

	public void setSpecialtySelectItem(SelectItem[] specialtySelectItem) {
		this.specialtySelectItem = specialtySelectItem;
	}

	public SelectItem[] getUserSelectItem() {
		if(userSelectItem == null)
			userSelectItem = new SelectItem[0];
		return userSelectItem;
	}

	public void setUserSelectItem(SelectItem[] userSelectItem) {
		this.userSelectItem = userSelectItem;
	}


	public String getRoleId() {
		return roleId;
	}


	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}


	public String getFacultyId() {
		if(facultyId == null || facultyId.equals("")){
			facultyId = CCPContants.getUserBean().getFacultyId();
		}
		return facultyId;
	}


	public void setFacultyId(String facultyId) {
		this.facultyId = facultyId;
	}


	public String getSpecialtyId() {
		return specialtyId;
	}


	public void setSpecialtyId(String specialtyId) {
		this.specialtyId = specialtyId;
	}


	public List<String> getUserList() {
		if(userList == null)
			userList = new ArrayList<String>();
		return userList;
	}


	public void setUserList(List<String> userList) {
		this.userList = userList;
	}
}
